package com.ztgf.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.dto.OrderCommodityRelationDTO;
import com.ztgf.order.models.dto.OrderInfoDTO;
import com.ztgf.order.models.dto.OrderStatusDTO;
import com.ztgf.order.models.service.IOrderCommodityRelationService;
import com.ztgf.order.models.service.IOrderInfoService;
import com.ztgf.order.models.service.IOrderStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 订单商品表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-03 15:55:40
 */

@RestController
@RequestMapping("/api/orderCommodityRelation")
@Slf4j
@Api(value = "OrderCommodityRelationApiController", tags = "订单商品表Api接口")
public class OrderCommodityRelationApiController {

    @DubboReference
    private IOrderCommodityRelationService orderCommodityRelationService;
    @DubboReference
    private IOrderStatusService orderStatusService;
    @PostMapping(value = "/updateOrderGoodsPrice")
    @ApiOperation(value = "修改订单商品实付金额", notes = "修改订单商品实付金额")
    public Result updateOrderGoodsPrice(Long shopId, Long id, BigDecimal price, Integer type) {
        OrderCommodityRelationDTO orderCommodityRelationDTO = orderCommodityRelationService.selectById(id);
        if (orderCommodityRelationDTO!=null){
            EntityWrapper<OrderStatusDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("order_id",orderCommodityRelationDTO.getOrderId());
            OrderStatusDTO orderStatusDTO = orderStatusService.selectOne(wrapper);
            if (orderStatusDTO!=null){
                if (orderStatusDTO.getOrderState()!=0){
                    return new Result<>(CodeEnum.FAIL_100017);
                }
            }
        }
        if (type == 0) {
            OrderCommodityRelationDTO dto = orderCommodityRelationService.selectById(id);
            if (dto == null) {
                return new Result<>(CodeEnum.FAIL_100008);
            }
            if (price.compareTo(dto.getMoeny()) > 0) {
                return new Result<>(CodeEnum.FAIL_100018);
            }
            //普通商品改价
            boolean result = orderCommodityRelationService.updateOrderGoodsPrice(shopId, id, price);
            if (result) {
                return new Result<>();
            }
        } else if(type == 1){
            //询价商品改价
            boolean result = orderCommodityRelationService.updateOtherGoodsPrice(shopId, id, price);
            if (result) {
                return new Result<>();
            }
        }
        return new Result<>(CodeEnum.FAIL_100008);
    }
}
